<?php

/**
 * midia actions.
 *
 * @package    sispatr
 * @subpackage midia
 * @author     Foxsis
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class midiaActions extends sfActionsFoxsis
{
	public function executeIndex(sfWebRequest $request)
	{
		return $this->forward('midia', 'list');
	}

	public function executeList()
	{
		$paginaAtual = $this->_setActualPage();
		$this->filter = $this->_getFilter();
		$listQuery = MidiaTable::carregarMidia($this->filter);

		## cria o paginador
		$this->getContext()->getConfiguration()->loadHelpers('Url');
		$this->pager_layout = new Foxsis_Pager_Layout(
		$pager = new Doctrine_Pager(
		$listQuery,
		$paginaAtual,
		NUM_LIST_ITENS
		),
		new Doctrine_Pager_Range_Sliding(array('chunk' => MAX_PAGER_LINKS)),
		url_for('midia/list')
		);

		## executa a consulta
		$this->midia_list = $this->pager_layout->execute(array(), Doctrine::HYDRATE_ARRAY);

		$this->setTemplate('index');
	}

	public function executeFilter()
	{
		$filter = new Filtro(
		$this->getRequestParameter(sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE)
		);

		$this->getUser()->setAttribute(
		sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE,
		serialize($filter),
		$this->getModuleName()
		);

		return $this->redirect($this->getModuleName().'/list');
	}

	public function executeNew(sfWebRequest $request)
	{
		$this->ref_name     = $this->getRequestParameter("ref");
		$this->ref_value    = $this->getRequestParameter("ref_value");
		$this->title		= "Cadastro de arquivo";
		$this->form 		= new MidiaForm();
	}

	public function executeCreate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));

		$this->form = new MidiaForm();

		$this->processForm($request, $this->form);
		return sfView::NONE;
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($arquivo = Doctrine::getTable('Midia')->find(array($request->getParameter('id_midia'))), sprintf('Object midia does not exist (%s).', $request->getParameter('id_midia')));
		$this->form = new MidiaForm($arquivo);

		$this->processForm($request, $this->form);
		return sfView::NONE;
	}

	public function executeArquivos()
	{
		$this->ref_name     = $this->getRequestParameter("ref_name");
		$this->ref_value    = $this->getRequestParameter("ref_value");
		$this->hasmany      = $this->getRequestParameter("hasmany");

		$id_name    = $this->ref_name;

		$this->reference    = $id_name;

		$this->midia  = MidiaTable::carregarMidia(array($id_name=>$this->ref_value));

	}

	public function executeEdit(sfWebRequest $request)
	{
		$this->forward404Unless($arquivo = Doctrine::getTable('Midia')->find(array($request->getParameter('id_midia'))), sprintf('Object midia does not exist (%s).', $request->getParameter('id_midia')));

		$this->title		= "Edição de arquivo";
		$this->form 		= new MidiaForm($arquivo);
		$this->setTemplate('new');
	}



	public function executePreview()
	{
		$this->midia = Doctrine::getTable('Midia')->find(array($this->getRequestParameter('id_midia')));
	}

	public function executeDelete(sfWebRequest $request)
	{
		try
		{
			$request->checkCSRFProtection();

			$this->forward404Unless($arquivo = Doctrine::getTable('Midia')->find(array($request->getParameter('id_midia'))), sprintf('Object arquivo does not exist (%s).', $request->getParameter('id_midia')));
			$arquivo->delete();

			$this->redirect('midia/index');
		}
		catch(Excepiton $e)
		{
			$this->getUser()->setFlash('erro', $e->getMessage());
			$this->redirect('midia/index');
		}

	}

	protected function processForm(sfWebRequest $request, sfForm $form)
  	{
	  	try
	  	{
	    	$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

	    	if($this->getRequestParameter('ref_name'))
	  		{
		  		$data	= explode("_", $this->getRequestParameter('ref_name'));
		  		$id		= "set".ucfirst($data[0]).ucfirst($data[1]);
		  		$form->getObject()->$id($this->getRequestParameter("ref_value"));
	  		}

		    if ($form->isValid())
		    {
		      $midia = $form->save();
		      echo "Operação efetuada com sucesso";
		    }
		    else
	    		echo $form->getErrorSchema();
	    }
	    catch(Exception $e)
		{
			$this->getUser()->setFlash('erro', $e->getMessage());
		}
  	}
}
